package com.cinema.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinema.bean.Resource;
import com.cinema.service.ResourceService;
import com.cinema.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("resource")
@CrossOrigin
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("list")
    public Result getList() {
        List<Resource> list = resourceService.list();
        return Result.success(list);
    }

    @GetMapping("page")
    public Result getPage(Page page) {
        Page result = resourceService.page(page);
        return Result.success(result);
    }

    @PostMapping
    public Result add(@RequestBody Resource resource) {
        resourceService.save(resource);
        return Result.success("保存成功", resource);
    }

    @DeleteMapping("/{resource_id}")
    public Result delete(@PathVariable("resource_id") Long resourceId) {
        boolean b = resourceService.removeById(resourceId);
        if (b) {
            return Result.success("删除成功");
        } else {
            return Result.fail(201, "删除失败");
        }
    }

    @PutMapping
    public Result edit(@RequestBody Resource resource) {
        boolean b = resourceService.updateById(resource);
        if (b) {
            return Result.success("修改成功");
        } else {
            return Result.fail(201, "修改失败");
        }
    }
}